package team.hyznrj.studentsys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.Api;
import team.hyznrj.studentsys.dto.QualityAchievementDto;
import team.hyznrj.studentsys.dto.StudentMessageSearchDto;
import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.enums.DataBaseOperateMethod;
import team.hyznrj.studentsys.enums.DataReportEnum;
import team.hyznrj.studentsys.exception.AuthenticationOnFlagException;
import team.hyznrj.studentsys.exception.DataBaseOperationError;
import team.hyznrj.studentsys.exception.DataNullException;
import team.hyznrj.studentsys.service.ExcelService;
import team.hyznrj.studentsys.service.FileService;
import team.hyznrj.studentsys.service.QualityAchievementService;
import team.hyznrj.studentsys.utils.ReadExcel;

@RestController
@RequestMapping("Quality")
@Api(value = "Quality")
public class QualityAchievementController {
	@Autowired
	QualityAchievementService service;
	@Autowired
	FileService fileService;
	@Autowired
	ExcelService excelService;
	/**
	 * 界面接口:导入操行分表(多文件导入)
	 * @throws Exception 
	 * @throws AuthenticationOnFlagException 
	 * @throws DataNullException 
	 */
	@ResponseBody
	@PostMapping(value = "/importMsg")
	public Object importExcel(MultipartFile[] files,HttpServletRequest request) throws DataNullException, AuthenticationOnFlagException, Exception{
		String flag=(String) request.getAttribute("flag");
		List<String> fileUpload = fileService.fileUpload(files, ReadExcel.EXCEL_PREDICATE);
		Object saveExcelInGrade = excelService.saveExcelInQualityMarks(fileUpload, flag);
		return new DataReport(DataReportEnum.SUCCESS,null,saveExcelInGrade,true);
	}
	/**
	 * 界面接口:增加一条操行分记录
	 */
	@ResponseBody
	@RequestMapping(value="/addMsg",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object addMsg(QualityAchievementDto record,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException{
		String flag=(String) request.getAttribute("flag");
		boolean result = service.operateQuality(record, DataBaseOperateMethod.INSERT_AND_UPDATE, flag);
		return new DataReport(result? DataReportEnum.SUCCESS:DataReportEnum.INSERT_ERROR,null,result,result);
	}
	/**
	 * 界面接口:增加多条相同的操行分记录
	 */
	@ResponseBody
	@RequestMapping(value="/addMsgs",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object addMsgs(QualityAchievementDto record,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException{
		String flag=(String) request.getAttribute("flag");
		String result = service.addMsgs(record, flag);
		return new DataReport( DataReportEnum.SUCCESS,null,result,true);
	}
	/**
	 * 更新一条操行分记录
	 */
	@ResponseBody
	@RequestMapping(value="/updateMsg",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object updateMsg(QualityAchievementDto record,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException{
		String flag=(String) request.getAttribute("flag");
		boolean result = service.operateQuality(record, DataBaseOperateMethod.UPDATE, flag);
		 return new DataReport(result? DataReportEnum.SUCCESS:DataReportEnum.UPDATE_ERROR,null,result,result);
	}
	/**
	 * 删除一条操行分记录(根据id)
	 */
	@ResponseBody
	@RequestMapping(value="/deleteMsg",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object deleteMsg(QualityAchievementDto record,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException{
		String flag=(String) request.getAttribute("flag");
		boolean result= service.operateQuality(record, DataBaseOperateMethod.DELETE, flag);
		return new DataReport(result? DataReportEnum.SUCCESS:DataReportEnum.DELETE_ERROR,null,result,result);
	}
	/**
	 * 删除多条操行分记录()
	 */
	@ResponseBody
	@RequestMapping(value="/deleteMulityMsg",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object deleteMulityMsg(List<String> no,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException{
		String flag=(String) request.getAttribute("flag");
		int result= service.deleteMulityQualities(no,flag);
		return new DataReport(result>0? DataReportEnum.SUCCESS:DataReportEnum.DELETE_ERROR,null,result,result>0);
	}
	/**
	 * 查询详细信息(多条)
	 */
	@ResponseBody
	@RequestMapping(value="/searchMsg/detail",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object searchMsgDetail(StudentMessageSearchDto dto,HttpServletRequest request) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException{
		String flag=(String) request.getAttribute("flag");
		dto.setFlag(flag);
		Object result= service.selectByDetail(dto);
		return new DataReport(DataReportEnum.SUCCESS,"查询成功",result,true);
	}
	/**
	 * 查询简要信息
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/searchMsg/simple",method={RequestMethod.POST} ,produces ="application/json;charset=utf-8")
	public Object searchMsgSimple(StudentMessageSearchDto dto,HttpServletRequest request){
		String flag=(String) request.getAttribute("flag");
		dto.setFlag(flag);
		Object result= service.selectBySimple(dto);
		return new DataReport(DataReportEnum.SUCCESS,"查询成功",result,true);
	}
	
}
